

<?php


//make_all_master_details();




if (isPortalSuperAdmin()){

	if (getParameter("makedatabasebackup")=="ok")
	{
		system("cmd /c C:\wamp\www\butterfly\databasebackup.bat");
	}
	?>



<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Reset Application Data</a></li>
  <li><a href="#tabs-7">Portal Statistics</a></li>
  
    <li><a href="#tabs-2">Set Bulk preferences</a></li>
    <li><a href="#tabs-3">Fix Portlets Rights</a></li>
        <li><a href="#tabs-4">Fix Content Values</a></li>
            <li><a href="#tabs-5">Portal Synchronisation</a></li>
         <li><a href="#tabs-8">Database backup</a></li>
      <li><a href="#tabs-6">Delete Portal</a></li>
  </ul>


     <div class="cadre" id="tabs-8">
    <a href="<?=addParameter("makedatabasebackup", "ok","")?>">Backup database</a>
</div>

    <div class="cadre" id="tabs-1">
    <?php if (getParameter("ResetAppData")=="ok")
    {
    	//check captcha
    	
    	
    
    		if (!getParameter("captcha") || ( getParameter("captcha") && trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha'])) {
    			ShowError("Invalid captcha ! <a href='".$_SERVER['HTTP_REFERER']."'>Back</a>");
    		}else{
    			
			//do the job
		$sSql="SELECT
portal_app.lib,
portal_app.`name`
FROM
portal_portal_app
INNER JOIN portal_app ON portal_portal_app.appId = portal_app.id WHERE portalId=$portalId";


$rs=$GLOBALS["conn"]->Execute($sSql);


$GLOBALS["conn"]->Execute("SET FOREIGN_KEY_CHECKS=0;\nSET autocommit=0;\nSET unique_checks=0;");
 
while ($rs && !$rs->EOF)  {


	$appName=$rs->fields["name"];
		
 
    	$tableArray = getParameter($appName);
	//print_r(getParameter("portal"));
	if (count($tableArray)>0){
        foreach($tableArray as $table){
	
                ShowSuccess("Reseting Data On Table : $table.");
       	      $GLOBALS["conn"]->Execute("DELETE FROM $table WHERE portalId=$portalId;");
               
	}}

	$rs->MoveNext();
    		}
    		
    		
    	}
    	
$GLOBALS["conn"]->Execute("SET FOREIGN_KEY_CHECKS=1;\nSET autocommit=1;\nSET unique_checks=1;");
ShowSuccess("Data Reset done succesfully ! <a href='".$_SERVER['HTTP_REFERER']."'>Back</a>");
    	
    }else{
?>
    <form action="<?php echo createRelativeUrl("ResetAppData","ok"); ?>" method="POST">
        
    <?php 
    
    $sSql="SELECT
portal_app.lib,
portal_app.`name`
FROM
portal_portal_app
INNER JOIN portal_app ON portal_portal_app.appId = portal_app.id WHERE portalId=$portalId";
    

    $rs=$GLOBALS["conn"]->Execute($sSql);
    
    	
    while ($rs && !$rs->EOF)  {
    
    
    $appName=$rs->fields["name"];
    $appLib=$rs->fields["lib"];
    ?>
    <ul style="display: inline-block;"><li><h4><?=$appLib?></h4></li>
    <select multiple="multiple" style="width: 250px" name="<?=$appName?>[]">
    <option></option>
    <?php 
   
    
    $sSql="SELECT
information_schema.`TABLES`.TABLE_NAME as tablename
FROM
information_schema.`TABLES` where TABLE_NAME LIKE '".$appName."_%'";
        		
	$rs2=$GLOBALS["conn"]->Execute($sSql);
    
    

    while ($rs2 && !$rs2->EOF)  {
    	

$tablename=$rs2->fields["tablename"];

   ?>
  <option value="<?=$tablename?>"><?=$tablename?></option>
    <?php 

	$rs2->MoveNext();
    }
    	$rs->MoveNext();
    
    ?></select></ul><?php 
    }
    
    
    ?>
    
    <br clear="all"/>
    <div style="padding-left: 40px">
            Please type what you see in the image to confirme the operation.<br/><br/>
                <img src="/php/captcha/captcha.php" id="captcha" /><br/>



                <!-- CHANGE TEXT LINK -->
                <a href="#" onclick="
	    document.getElementById('captcha').src='/php/captcha/captcha.php?'+Math.random();
	    document.getElementById('captcha-form').focus();"
                   id="change-image">Not readable? Change text.</a><br/><br/>


                <input type="text" name="captcha" id="captcha-form" /><br/><br/>
                <input type="submit" value="Submit" name="submit">
                </div>
            </form>
            <?php } ?>
      <br clear="all"/>
</div>



    <div class="cadre" id="tabs-2">
<form action="/portlet.php" method="post" enctype="multipart/form-data" encoding="multipart/form-data">
    <input type="hidden" name="app" value="system">
    <input type="hidden" name="operation" value="setportletprefs">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000">
    File: <input type="file" name="file"><br/>
    Name : <input type="text" name="name" value=""><br/>
    Value : <input type="text" name="value" value=""><br/>
    <input type="submit" value="Execute"><br/>

</form>
    </div>

    <div class="cadre" id="tabs-3">
    <a href="/portlet.php?app=system&operation=fixportletrights&groups=Anonymous">Anonymous</a><br/>
</div>

  
    <div class="cadre" id="tabs-4">
        <a href="/portlet.php?app=system&operation=fixcontentvalues">Execute</a><br/>
    </div>


   
    <div class="cadre" id="tabs-5">


        <form action="/portlet.php" method="post">
            <input type="hidden" name="app" value="system">
            <input type="hidden" name="operation" value="exportportal">

            Import   : <input type="radio" name="import" value="true" checked> True <input type="radio" name="import" value="false" checked> False<br/>
            Override : <input type="radio" name="cleanexport" value="true" checked> True <input type="radio" name="cleanexport" value="false" checked> False<br/>
            Target PortalId : <input type="text" name="targetPortalId" value="" ><br/>
            <input type="submit" value="Execute"><br/>

        </form>

        </div>

    <?php
    if (getParameter("operation")=="deletePortal")
    {
        //echo "this portal will be deleted";
      


            if (isset($_REQUEST['captcha'])) {
                if (trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha']) {
                    ShowError("Invalid captcha !");
                }else{
                    echo "portal deleted!";
                    deletePortal(getPortalUrl());

                }
            }
    
    }else{
        if (getCurrentUserLevel()<=0) {
            ?>

<div class="cadre" id="tabs-6">

        <div class="extrafunctionx" >
            <form action="<?php echo createRelativeUrl("confirm","ok").addParameter("operation","deletePortal"); ?>" method="POST">
            <?=Captcha()?>
      <br/><br/>
                <input type="submit" value="Submit" name="submit">
            </form>
        </div>
  
</div>
        <?php }

    }
    ?>
<br/>
<?php } ?>


<script>
  $(function() {
    $( "#tabs" ).tabs();
  });
  </script>
<div id="tabs-7" class="cadre">




  <?php

if (false) {
define('ga_email','mohamed.zeouine@gmail.com');
define('ga_password','');
//echo getObjByKey("portal","googleAnalytics","id",$portalId);
$ga = new gapi(ga_email,ga_password);
define('ga_profile_id',$ga->getProfileId(getObjByKey("portal","googleAnalytics","id",$portalId)));





$ga->requestReportData(ga_profile_id,array('browser'),array('pageviews','visits'));
$values=array();
$labels=array();
$colors=array();

    $i=0;
    foreach($ga->getResults() as $result){
        $values[$i]=$result->getVisits();
        $colors[$i]=rand_colorCode();
        $labels[$i++]=$result;
        ?>

        <?php
   }
    ?>


<div class="portalstats">
<h3>Browsers Stats</h3>
<?php
$piChart = new gPie3DChart();
$piChart->addDataSet($values);
$piChart->setLegend($labels);
//$piChart->setLabels($labels);
$piChart->setColors($colors);
?>
<img src="<?php print $piChart->getUrl();  ?>" />
</div>
<?php
$ga->requestReportData(ga_profile_id,array('keyword'),array('visits'),'-visits');
$values=array();
$labels=array();
$colors=array();
$gaResults = $ga->getResults();

$i=0;

foreach($gaResults as $result)
{

    $colors[$i]=rand_colorCode();
    $values[$i]=$result->getVisits();
    $labels[$i++]= $result->getKeyword();
}


?>
<div class="portalstats">
<h3>Keyword Search</h3>
<?php
$piChart = new gPie3DChart();
$piChart->addDataSet($values);
$piChart->setLegend($labels);
//$piChart->setLabels($labels);
$piChart->setColors($colors);
?>
<img src="<?php print $piChart->getUrl();  ?>" />
    </div>

<?php
$today = date('Y-m-d');
$startdate=date('Y-m-d',strtotime("-7 day"));
$ga->requestReportData(ga_profile_id,array('source'),array('visits'),array('-visits'),null,$startdate,$today,1,100);
$values=array();
$labels=array();
$colors=array();
$gaResults = $ga->getResults();

$i=0;

foreach($gaResults as $result)
{

    $colors[$i]=rand_colorCode();
    $values[$i]=$result->getVisits();
    $labels[$i++]= $result->getSource();
}


?>
<div class="portalstats">
    <h3>Last Week Visits</h3>
    <?php
    $piChart = new gPie3DChart();
    $piChart->addDataSet($values);
    $piChart->setLegend($labels);
//$piChart->setLabels($labels);
    $piChart->setColors($colors);
    ?>
    <img src="<?php print $piChart->getUrl();  ?>" />
</div>
    <br/><br/>
Total visits last week :<?php echo $ga->getVisits(); ?>


<br/><br/>
<?php } ?>

  
</div>
</div>